<template>
    <div>
      <el-card class="box-card">
        <el-row>
          <el-col :span="11">
            可开发票总金额{{money}}
          </el-col>

        </el-row>

        <el-form :model="forms" ref="forms" label-width="80px"  :rules="rules">
          <el-form-item label="发票抬头" >
            <el-input v-model="forms.invoiceTitle" style="width: 200px;"></el-input>
          </el-form-item>
          <el-form-item label="税号">
            <el-input v-model="forms.invoiceNum" style="width: 200px;"></el-input>
          </el-form-item>
          <el-form-item label="发票类型">
            <el-select v-model="forms.invoiceType" >
              <el-option label="普通发票"   :value="1"></el-option>
              <el-option label="增值税发票"   :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="税率">
            <el-input v-model="forms.taxRate" style="width: 200px;"></el-input>
          </el-form-item>
          <el-form-item label="开票金额" prop="amount">
            <el-input v-model.number="forms.amount" style="width: 200px;"></el-input><span v-if="this.money<forms.amount" style="color: #ec5a5b">超过可开票金额</span>
          </el-form-item>
          <el-form-item label="发票内容" >
            <el-input type="textarea" v-model="forms.content"  style="width: 300px;"></el-input>
          </el-form-item>
          <el-form-item label="收件人" >
            <el-input v-model="forms.receiver" style="width: 200px;"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" >
            <el-input v-model="forms.tel" style="width: 200px;"></el-input>
          </el-form-item>
          <el-form-item label="收件地址" >
            <el-input v-model="forms.revAddress" style="width: 200px;"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit()">保存</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>


      </el-card>
    </div>
</template>

<script>
    export default {
        name: "Receipt",
      data(){
        var checkAge = (rule, value, callback) => {
          if (!value) {
            return callback(new Error('发票金额不能为空'));
          }
          setTimeout(() => {
            if (!Number.isInteger(value)) {
              callback(new Error('请输入数字值'));
            } else {
              if (value <0) {
                callback(new Error('金额不能小于0'));
              } else {
                callback();
              }
            }
          }, 1000);
        };
          return{
            forms:{tel:"",amount:"",textarea:''},
            money:'',
            orderid:'',
            rules:{
              amount: [
                { validator: checkAge, trigger: 'blur' },
              ],

            }
          }

      },
      methods:{
          searchList:function () {
            this.forms.invoiceTitle=JSON.parse(sessionStorage.getItem("customer")).invoiceType;
            this.forms.invoiceNum=JSON.parse(sessionStorage.getItem("customer")).invoiceNum;
            this.forms.invoiceType=JSON.parse(sessionStorage.getItem("customer")).invoiceType;
            this.forms.taxRate=JSON.parse(sessionStorage.getItem("customer")).taxRate;
            this.forms.customerId=JSON.parse(sessionStorage.getItem("customer")).id;
            this.forms.receiver=JSON.parse(sessionStorage.getItem("linkman")).name;
            this.forms.tel=JSON.parse(sessionStorage.getItem("linkman")).tel;
            this.forms.revAddress=JSON.parse(sessionStorage.getItem("linkman")).recvAddress;

          },
        //保存发票
        onSubmit:function () {
          this.$refs['forms'].validate((valid) => {
            if (valid) {
              this.$http.post("/invoice/save/" + this.orderid, this.forms).then((res) => {
                if (res.data.success) {
                  alert(res.data.message);
                  this.$router.push("invoiceList");
                } else {
                  alert(res.data.message);
                }
              });
            }
          })


        }
      },
      mounted() {
        this.searchList();
        this.orderid=this.$route.query.orderid;
        this.$http.get("/invoice/getMoney/"+JSON.parse(sessionStorage.getItem("customer")).id).then((res)=>{
          this.money=res.data.result;
        });
      }
    }
</script>

<style scoped>

</style>
